import { Injectable,ExecutionContext,CanActivate} from '@nestjs/common';
import { Observable } from 'rxjs';
import { JwtService } from '@nestjs/jwt';
import config from 'src/config/index';


@Injectable()
export class NoAuthGuard implements CanActivate {
	canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
		// 判断是否携带jwt
		const request = context.switchToHttp().getRequest();
		const token = request.headers.authorization
		if(token&&token.startsWith('Bearer ')){
			var secret=""
			if(request.route.path.startsWith('/admin')){
				secret=config.admin.secret
			}
			if(request.route.path.startsWith('/app')){
				secret=config.app.secret
			}
			var jwt=new JwtService({secret:secret})
			var decode=jwt.decode(request.headers.authorization.replace('Bearer ',''))
			request.user=decode
		}
		return true;
	}
}
